package cn.zlj.pethome.pet.service.impl;

import cn.zlj.pethome.pet.domain.PetType;
import cn.zlj.pethome.pet.service.IPetTypeService;
import cn.zlj.pethome.basic.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zlj
 * @since 2022-10-28
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {

    @Override
    public List<PetType> tree() {
        List<PetType> result=new ArrayList<>();
        List<PetType> all = super.findAll();
        Map<Long, PetType> collect = all.stream().collect(Collectors.toMap(PetType::getId, e -> e));
        all.forEach(petType -> {
           if(petType.getPid()==null){
               result.add(petType);
           }else {
               PetType type = collect.get(petType.getPid());
               if(type==null){
                 return;
               }
               type.getChildren().add(petType);
           }
        });
        return result;
    }
}
